package com.sinping.qh.api.admin.statistics;

import com.sinping.qh.api.admin.base.BaseAdminEndPoint;
import com.sinping.qh.api.exception.HandlerException;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.common.CommonParamDTO;
import com.sinping.qh.service.admin.IHiddenDangerRecordService;
import com.sinping.qh.service.admin.lab.ILabInspectItemTreeService;
import com.sinping.qh.service.admin.statistics.IStatisticsService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "Admin统计分析", description = "Admin统计分析")
@RestController
@RequestMapping(value = Const.ADMIN_URI,name = "Admin统计分析")
public class StatisticsEndPoint extends BaseAdminEndPoint{

    @Autowired
    ILabInspectItemTreeService iLabInspectItemTreeService;

    @Autowired
    IHiddenDangerRecordService iHiddenDangerRecordService;

    @Autowired
    IStatisticsService iStatisticsService;

    /*
     * 危险源分布统计
     * */
    @ApiOperation(value = "危险源分布统计")
    @RequestMapping(value = "statistics/danger_source",method = RequestMethod.GET)
    public ResDto getDangerStatisticsList(){
        try {
            return iLabInspectItemTreeService.getDangerStatisticsList();
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.SERVER_ERROR,"数据解析异常");
        }

    }
    /*
     * 按院系统计危险源分布实验室数量
     * */
    @ApiOperation(value = "按院系查找危险源的实验室数量分布统计")
    @RequestMapping(value = "statistics/danger_source/{unitId}",method = RequestMethod.GET)
    public ResDto getDangerStatisticsListByUnitId(@ApiParam(name = "unitId",value = "123") @PathVariable String unitId){
        try {
            if (!StringUtils.isNotBlank(unitId)) {
                return new ResDto(Const.PARAM_ERROR, "院系id不能为空");
            }
            return iLabInspectItemTreeService.getDangerStatisticsListByUnitId(unitId);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.SERVER_ERROR,"数据解析异常");
        }

    }

    /**
     * 统计隐患记录隐患数
     */
    @ApiOperation(value = "隐患分布统计")
    @RequestMapping(value = "statistics/hidden_danger",method = RequestMethod.GET)
    public ResDto getHiddenDangerRecordStatistics(@ApiParam(name = "msg",value = "{\"level\":\"1\",\"show_all\":\"1\",\"check_item_id\":\"123\",\"start_date\":\"2018-06-10 00:00:00\",\"end_date\":\"2018-07-10 00:00:00\"}") @RequestParam(required = true) String msg){
        try {
            if (StringUtils.isBlank(msg)){
                return new ResDto(Const.PARAM_ERROR, "请传值");
            }
            return iHiddenDangerRecordService.getHiddenDangerRecordStatistics(msg);
        }
        catch (Exception e){
            e.printStackTrace();
            return new ResDto(Const.SERVER_ERROR,"数据解析异常");
        }

    }

    /**
     * 隐患统计
     * @return
     */
    @ApiOperation(value = "隐患统计")
    @RequestMapping(value = "statistics/hidden_statistics",method = RequestMethod.GET)
    public ResDto getHiddenDangerNumber(@ApiParam(name = "msg",
            value = "{\"unit_id\":\"123\",\"start_date\":\"2018-06-10 00:00:00\",\"end_date\":\"2018-07-10 00:00:00\"}")
                                            @RequestParam(required = true) String msg){
        ResDto resDto =new ResDto(ConstEum.SUCCESS);

        CommonParamDTO commonParamDTO=JsonMapper.defaultMapper().fromJson(msg, CommonParamDTO.class);

        checkNull(commonParamDTO,ConstEum.PARAM_ERROR);

        String ret=iStatisticsService.getHiddenDangerNumber(commonParamDTO);
        if(StringUtils.isBlank(ret))
        {
            throw new HandlerException(ConstEum.FAIL,ConstEum.FAIL.getMessage());
        }
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(ret);
        return resDto;
    }

    /**
     * 隐患按年统计
     * @return
     */
    @ApiOperation(value = "隐患按年统计")
    @RequestMapping(value = "statistics/hidden_statistics_year",method = RequestMethod.GET)
    public ResDto getHiddenDangerNumberByYear(@ApiParam(name = "msg",
            value = "{\"unit_id\":\"123\",\"start_year\":\"2017\",\"end_year\":\"2018\"}")
                                        @RequestParam(required = true) String msg){
        ResDto resDto =new ResDto(ConstEum.SUCCESS);

        CommonParamDTO commonParamDTO=JsonMapper.defaultMapper().fromJson(msg, CommonParamDTO.class);

        checkNull(commonParamDTO,ConstEum.PARAM_ERROR);

        String ret=iStatisticsService.getHiddenDangerNumberByYear(commonParamDTO);
        if(StringUtils.isBlank(ret))
        {
            throw new HandlerException(ConstEum.FAIL,ConstEum.FAIL.getMessage());
        }
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(ret);
        return resDto;
    }
//    /**
//     * 统计隐患记录隐患数
//     */
//    @ApiOperation(value = "整改率分布统计")
//    @RequestMapping(value = "statistics/hidden_danger_improve",method = RequestMethod.GET)
//    public ResDto getHiddenDangerRecordImproveStatistics(@ApiParam(name = "msg",value = "{\"level\":\"1\",\"check_item_id\":\"123\",\"sort\":\"-1\",\"start_date\":\"2018-06-10 00:00:00\",\"end_date\":\"2018-07-10 00:00:00\"}") @RequestParam(required = true) String msg){
//        try {
//            if (StringUtils.isBlank(msg)){
//                return new ResDto(Const.PARAM_ERROR, "请传值");
//            }
//            return iHiddenDangerRecordService.getHiddenDangerRecordImproveStatistics(msg);
//        }
//        catch (Exception e){
//            e.printStackTrace();
//            return new ResDto(Const.SERVER_ERROR,"数据解析异常");
//        }
//
//    }
}
